package com.mobile2you.nci.android.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.widget.ViewFlipper;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.mobile2you.nci.android.domain.Centres;

/**
 * The icon marker on map. Manages a set of Overlay to draw agency pins on map.
 * 
 * @author JBromo
 * 
 */

@SuppressWarnings("rawtypes")
public class MarkerTicket extends ItemizedOverlay {

	private final List<Centres> golfLieu = new ArrayList<Centres>();
	private final ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

	Context context;

	MapView mapView;

	ViewFlipper viewFlipper;

	BulleDialog.onBullClickListener listener;

	/**
	 * 
	 * @param defaultMarker
	 * @param context
	 * @param type
	 *            represent the type of map if it is itineraie , listagence.
	 */
	public MarkerTicket(Drawable defaultMarker, Context context,
			MapView mapView, BulleDialog.onBullClickListener listener) {
		super(boundCenterBottom(defaultMarker));
		this.mapView = mapView;
		this.context = context;
		this.listener = listener;

	}

	public void addOverlay(OverlayItem overlay, Centres golfShop) {
		golfLieu.add(golfShop);
		mOverlays.add(overlay);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	/** Draw marker without shadow */
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, false);
	}

	/**
	 * Control a click to show a bulle info by user clicking in button
	 */

	@Override
	protected boolean onTap(int index) {

		Centres ticket = golfLieu.get(index);
		if (ticket != null) {
			BulleDialog bulleDialog = new BulleDialog(context, ticket, listener);
			bulleDialog.show();
			GeoPoint point = new GeoPoint((int) (ticket.getLatitude() * 1E6),
					(int) (ticket.getLongitude() * 1E6));
			mapView.getController().setCenter(point);
			return super.onTap(index);
		}
		return false;

	}

	@Override
	public int size() {
		return mOverlays.size();
	}

}
